﻿/*
	Copyright (c) 2013 Andrew Meyer

	Permission is hereby granted, free of charge, to any person obtaining a copy of this software
	and associated documentation files (the "Software"), to deal in the Software without
	restriction, including without limitation the right to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all copies or
	substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
	BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
	DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using Ascend;

namespace AscendViewer
{
	public class ModelContentNodeViewModel : ContentNodeViewModel
	{
		#region Lifecycle
		public ModelContentNodeViewModel(ModelSceneNode modelSceneNode,
			Queue<TransformableObject> axisSceneAliases,
			Queue<TransformableObject> boneSceneAliases, ContentExplorerViewModel container,
			ContentNodeViewModel parent)
			: base(modelSceneNode, axisSceneAliases, boneSceneAliases, container, parent)
		{
			Name = modelSceneNode.Name;

			InitializeChildren(modelSceneNode.Children.Cast<TransformableObject>(),
				axisSceneAliases, boneSceneAliases, container);

			// Setup double-click handler
			modelSceneNode.MouseDoubleClick += (sender, e) =>
				{
					// Select this content node
					container.SelectedContentNode = this;

					// Point camera to node
					container.MainWindow.MainScene.LookAt(modelSceneNode);

					// Mark as handled so ancestors are not selected
					e.Handled = true;
				};

			// Create axes overlays
			_axesNormal = CreateAxes();
			_axesSelected = CreateAxes(0.05);

			// Show axes
			AxisOverlayState = OverlayState.Normal;
		}
		#endregion

		#region Private Members
		private ModelSceneNode _axesNormal;
		private ModelSceneNode _axesSelected;
		#endregion

		#region Public Properties
		private OverlayState _axisOverlayState;
		public OverlayState AxisOverlayState
		{
			get
			{
				return _axisOverlayState;
			}

			set
			{
				if (_axisOverlayState == value)
				{
					return;
				}

				// Remove old axes, if any
				if (_axisOverlayState != OverlayState.Hidden)
				{
					_axesNormal.Parent = null;
					_axesSelected.Parent = null;
				}

				switch (value)
				{
					// Replace with normal axes
					case OverlayState.Normal:
						_axesNormal.Parent = (SceneNode)_axisSceneAlias;
						break;

					// Replace axes with bold axes
					case OverlayState.Selected:
						_axesSelected.Parent = (SceneNode)_axisSceneAlias;
						break;
				}

				_axisOverlayState = value;
			}
		}
		#endregion

		#region Protected Methods
		protected override void SetupSceneAliases(Queue<TransformableObject> axisSceneAliases,
			Queue<TransformableObject> boneSceneAliases)
		{
			base.SetupSceneAliases(axisSceneAliases, boneSceneAliases);

			// Hide geometry
			((ModelSceneNode)_axisSceneAlias).Model = null;
			((ModelSceneNode)_boneSceneAlias).Model = null;
		}

		protected override void HandleSelectionChange()
		{
			base.HandleSelectionChange();

			if (IsSelected)
			{
				AxisOverlayState = OverlayState.Selected;
			}
			else
			{
				AxisOverlayState = OverlayState.Normal;
			}
		}
		#endregion
	}
}
